﻿package {
	import org.flixel.*;
	public class Player extends FlxSprite {
		[Embed(source = "../images/player_small.png")] public static var player_img:Class;

		private const PLAYER_SPEED:int = 70;
		private const PLAYER_DRAG:int = 175;
		public var charge:Number;
		private const STRONG_CHARGE:int = 20;
		private const SPECIAL_CHARGE:int = 33;
		public var currentWeapon:Weapon;
		private var charging:Boolean;
		
		public function Player():void {
			super();
			loadGraphic(player_img, true, true, 16, 16);
			drag.x = PLAYER_DRAG;
			drag.y = PLAYER_DRAG;
			charge = 0;
			charging = false;
			health = 6;

		}
		override public function update():void {
			handle_input();		
			if (charging) {
				charge += FlxG.elapsed * 10;
				FlxG.log(charge);
			}
			super.update();
		}

		public function equip(weapon:Weapon):void {
			currentWeapon = weapon;
		}

		private function handle_input():void {
			if (FlxG.keys.S) {
				velocity.y = PLAYER_SPEED;
				facing = DOWN;
			} if (FlxG.keys.W) {
				velocity.y = -PLAYER_SPEED;
				facing = UP;
			} if (FlxG.keys.D) {
				velocity.x = PLAYER_SPEED;
				facing = RIGHT;
			} if (FlxG.keys.A) {
				velocity.x = -PLAYER_SPEED;
				facing = LEFT;
			}
			if (FlxG.keys.justPressed("UP") || FlxG.keys.justPressed("DOWN") || FlxG.keys.justPressed("RIGHT") || FlxG.keys.justPressed("LEFT")) {
				charging = true;
			}
			if (charging) {
				if (FlxG.keys.justReleased("UP")) {
					if (charge < STRONG_CHARGE) {
						currentWeapon.weakAttack(FlxSprite.UP);
					} else if (charge >= STRONG_CHARGE && charge < SPECIAL_CHARGE) {
						currentWeapon.strongAttack(FlxSprite.UP);
					} else if (charge >= SPECIAL_CHARGE) {
						currentWeapon.specialAttack(FlxSprite.UP);
					}
					charge = 0;	
					charging = false;
				}	
				if (FlxG.keys.justReleased("DOWN")) {
					if (charge < STRONG_CHARGE) {
						currentWeapon.weakAttack(FlxSprite.DOWN);
					} else if (charge >= STRONG_CHARGE && charge < SPECIAL_CHARGE) {
						currentWeapon.strongAttack(FlxSprite.DOWN);
					} else if (charge >= SPECIAL_CHARGE) {
						currentWeapon.specialAttack(FlxSprite.DOWN);
					}
					charge = 0;	
					charging = false;
				}		
				if (FlxG.keys.justReleased("RIGHT")) {
					if (charge < STRONG_CHARGE) {
						currentWeapon.weakAttack(FlxSprite.RIGHT);
					} else if (charge >= STRONG_CHARGE && charge < SPECIAL_CHARGE) {
						currentWeapon.strongAttack(FlxSprite.RIGHT);
					} else if (charge >= SPECIAL_CHARGE) {
						currentWeapon.specialAttack(FlxSprite.RIGHT);
					}
					charge = 0;	
					charging = false;
				}
				if (FlxG.keys.justReleased("LEFT")) {
					if (charge < STRONG_CHARGE) {
						currentWeapon.weakAttack(FlxSprite.LEFT);
					} else if (charge >= STRONG_CHARGE && charge < SPECIAL_CHARGE) {
						currentWeapon.strongAttack(FlxSprite.LEFT);
					} else if (charge >= SPECIAL_CHARGE) {
						currentWeapon.specialAttack(FlxSprite.LEFT);
					}
					charge = 0;	
					charging = false;
				}
			}
		}

	}
}

